package main

import (
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/registry/etcdv3/v2"
	ocplugin "github.com/micro/go-plugins/wrapper/trace/opentracing/v2"
	"github.com/opentracing/opentracing-go"
	"log"
	"micro.hotel.client/controller"
	"micro.hotel.client/plugins"
	"time"
)

var (
	Endpoint = []string{"127.0.0.1:2379", "127.0.0.1:22379", "127.0.0.1:32379"}
)

func main() {
	tracer, closer, err := plugins.NewJaegerTracer("go.micro.api.hotel", "127.0.0.1:6831")
	if err != nil {
		log.Fatal("NewTracer error:", err)
		return
	}
	defer closer.Close()
	opentracing.SetGlobalTracer(tracer)

	reg := etcdv3.NewRegistry(func(opts *registry.Options) {
		// etcd 集群地址
		opts.Addrs = Endpoint
	})

	// micro服务
	service := micro.NewService(
		micro.Name("go.micro.api.hotel"),
		micro.Registry(reg),
		micro.RegisterTTL(time.Second*30),      // 服务注册TTL生存期
		micro.RegisterInterval(time.Second*10), // N秒重新注册一次
		micro.WrapClient(ocplugin.NewClientWrapper(tracer)),
	)

	service.Init()

	_ = service.Server().Handle(
		service.Server().NewHandler(new(controller.HotelController)),
	)
	_ = service.Server().Handle(
		service.Server().NewHandler(new(controller.NoticeController)),
	)

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
